package com.l024.android_ljl.view.login;


import android.text.TextUtils;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;

import com.l024.android_ljl.MainActivity;
import com.l024.android_ljl.R;
import com.l024.android_ljl.cache.CacheManager;
import com.l024.android_ljl.databinding.ActivityStartBinding;
import com.quantstack.llcommonlibrary.base.BaseNullActivity;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 启动页面
 */
public class StartActivity extends BaseNullActivity<ActivityStartBinding> {

    private AnimationSet animationSet;
    @Override
    public int getLayoutId() {
        return R.layout.activity_start;
    }

    @Override
    public void afterCreate() {
        //addStatusViewWithColor(this,getResources().getColor(R.color.white));
        animationSet = new AnimationSet(true);
        AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
        ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animationSet.addAnimation(alphaAnimation);
        animationSet.addAnimation(scaleAnimation);
        animationSet.setDuration(2000);
        animationSet.setRepeatCount(1);
        animationSet.setInterpolator(new LinearInterpolator());
        mViewDataBind.ivBg.setAnimation(animationSet);
        //3秒后跳转
        Observable.timer(3, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Long aLong) {
                        //跳转判断
                        String cacheToken = CacheManager.getInstance().getCacheToken();
                        if(TextUtils.isEmpty(cacheToken)){
                            go(StartActivity.this, LoginActivity.class);
                        }else{
                            go(StartActivity.this, MainActivity.class);
                        }
                        finish();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(animationSet!=null){
            animationSet.cancel();
            animationSet = null;
        }
    }
}